package com.xw.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xw.constant.RedisKeyConstant;
import com.xw.dao.PostCodeDao;
import com.xw.dao.ThumbsDao;
import com.xw.entity.PostCode;
import com.xw.exp.ServiceException;
import com.xw.req.PageRequest;
import com.xw.service.PostCodeService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 针对表【PostCode】的数据库操作Service实现
 * @Author: 72478
 * @Date: 2024/12/18 16:53
 * @Version: 1.0
 */
@Service("postCodeService")
public class PostCodeServiceImpl implements PostCodeService {
    @Resource
    private PostCodeDao postCodeDao;

    @Resource
    private ThumbsDao thumbsDao;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    /**
    * 分页查询
    *
    * @return 查询结果
    */
    @Override
    public PageInfo<PostCode> queryByPage(PageRequest<PostCode> pageRequest) {
        //开始分页
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<PostCode> list = this.postCodeDao.queryAll(pageRequest.getQ());
        PageInfo<PostCode> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void delete(Integer id) {
        // 1. 删除帖子
        this.postCodeDao.deleteById(id);
        // 2. 删除帖子评论和回复
        thumbsDao.deleteByPostId(id);
        // 3.清空redis缓存
        String cacheKey = RedisKeyConstant.REDIS_KEY_HIGH_RANK_POSTS;
        redisTemplate.delete(cacheKey);
    }
}
